<template>
	<v-dialog v-model="activeAndonDialogModel.andonOperationDialog" persistent max-width="1000px">
		<v-card>
			<v-card-title>
				<span class="text-h5">录入异常信息</span>
			</v-card-title>
			<v-card-text>
				<v-container>
					<v-row>
						<v-col cols="12" sm="6" md="3">
							<v-select @input="andonTypeChangeEvent" v-model="andon.type"
								:items="AndonExceptionTypeEnumList" label="异常类型" dense outlined></v-select>
						</v-col>
						<v-col cols="12" sm="6" md="3">
							<!-- <v-select v-model="andon.departmentid" :disabled="andon.type==null" :items="departmentList"
								label="选择责任部门" dense outlined></v-select> -->
							<v-text-field label="责任部门" v-model="department" :disabled="andon.type==null" dense outlined>
							</v-text-field>
						</v-col>
						<v-col cols="12" sm="6" md="3">
							<!-- <v-select v-model="andon.responsibleuserid" :disabled="andon.type==null" :items="userList"
								label="选择责任人" dense outlined>
							</v-select> -->
							<v-text-field label="责任人" v-model="user" :disabled="andon.type==null" dense outlined>
							</v-text-field>
						</v-col>

						<v-col cols="12" sm="6" md="3">
							<v-text-field label="不合格数量" v-model="andon.exceptioncount" :rules="qtyRule" dense outlined>
							</v-text-field>
						</v-col>

						<v-col cols="12">
							<uploadfile :UpLoad="andonUpLoadModel" :callBackPath="andonUpLoadCallBack"></uploadfile>
						</v-col>
						<v-col cols="12">
							<v-textarea outlined name="input-7-4" label="异常描述" v-model="andon.exceptionmessage"
								:value="andon.exceptionmessage">
							</v-textarea>
						</v-col>
					</v-row>
				</v-container>
			</v-card-text>
			<v-card-actions>
				<v-spacer></v-spacer>
				<v-btn color="blue darken-1" text @click="addAndonSubmit">
					确定
				</v-btn>
				<v-btn color="blue darken-1" text @click="cannel">
					退出
				</v-btn>
			</v-card-actions>
		</v-card>

	</v-dialog>
</template>

<script>
	import uploadfile from '../UploadFile.vue'
	export default {
		name: 'andon_operation',
		components: {
			uploadfile,
		},
		props: {
			activeAndonDialogModel: {
				andonOperationDialog: false,
				childrenid: 0,
				controllerName: ''
			},
		},

		data() {
			return {
				tempAndon: {},
				andon: {},
				pramterEnums: ['AndonExceptionType'],
				AndonExceptionTypeEnumList: [],
				department: null,
				user: null,
				qtyRule: [
					v => /^[0-9]*$/.test(v) || '请输入合法数字',
					v => v >= 0 || '需求量不能小于0'
				],
				andonUpLoadModel:{
					isUploadSingleImage: false,
					isUploadMultipleFiles: true,
					type: 10
				},
			}
		},
		watch: {
			activeAndonDialogModel: {
				handler(newVal) {
					this.$emit('update:activeAndonDialogModel', newVal)
					this.loadEnumList()
				},
				deep: true
			},
		},
		methods: {
			loadEnumList() {
				if (!this.activeAndonDialogModel.andonOperationDialog)
					return
				var that = this
				this.$httpRequest({
					url: `MES/MESCommon/GetTypes`,
					method: 'post',
					data: this.pramterEnums
				}).then(function(response) {
					if (response.status == 1) {
						that.AndonExceptionTypeEnumList = response.data
					} else {
						that.$message.error('加载配置信息失败')
					}
				})
			},
			
			async andonTypeChangeEvent(type) {
				var response = await this.loadDepartmentList(type)
				if (response.status == 1) {
					if (response.data == null) {
						return
					}
					this.department = response.data.name
					this.andon.departmentname = response.data.name
					this.andon.departmentid = response.data.id
					if (response.data.userList != null && response.data.userList.length > 0) {
						var userModel = response.data.userList.shift()
						this.user = userModel.realname
						this.andon.responsibleusername = userModel.realname
						this.andon.responsibleuserid = userModel.id
						this.andon.responsibleemail = userModel.email
					}
				}
			},
			//加载所有部门
			async loadDepartmentList(type) {
				this.user = null
				this.department = null
				this.andon.departmentname = ''
				this.andon.departmentid = 0
				this.andon.responsibleusername = ''
				this.andon.responsibleuserid = 0
				this.andon.responsibleemail = ''
				var response = await this.$httpRequest({
					url: `MES/${this.activeAndonDialogModel.controllerName}/GetDepartmentByAndonExceptionType?type=${type}`,
					method: 'get'
				})
				return response
			},
			
			async addAndonSubmit() {
				this.andon.childrenid = this.activeAndonDialogModel.childrenid
				this.andon.files = null
				var response = await this.$httpRequest({
					url: `MES/${this.activeAndonDialogModel.controllerName}/Add`,
					method: 'post',
					data: this.andon,
				})
				if (response.status == 1) {
					this.$message.success(response.info)
					this.activeAndonDialogModel.andonOperationDialog = false
					this.andon = {}
				} else {
					this.$message.error('添加异常处理单失败！')
				}
			},
			cannel() {
				this.activeAndonDialogModel.andonOperationDialog = false
				this.andon = {}
			},
			andonUpLoadCallBack(pathList){
					this.andon.url = JSON.stringify(pathList)
			},
		}
	}
</script>

<style>
</style>
